import json
import random

import pandas as pd

from util.common import *

if __name__ == '__main__':

    list = []

    df = pd.read_excel("source/tf_smp_web_asset2.xlsx")
    filtered_df = df[df['PUBLIC_WEB_LOGIN_URL'].notna()]

    for createTime in generate_workdays('2024-01-01', '2024-11-28', 1):
        id = "COMPZX" + random_string(14)

        timeSpan = random.randrange(1, 11)
        start_date_str, end_date_str = get_random_date_str(createTime, timeSpan)

        random_record = filtered_df.sample(n=1)

        assetId = random_record['ID'].item()
        url = random_record["PUBLIC_WEB_LOGIN_URL"].item()
        seep = {
            "taskId": id,
            "taskName": f"内容合规检测{createTime[:10]}",
            "taskSource": "2",
            "taskFlag": "1",
            "mainTaskId": None,
            "taskCreator": "PORT6452766823027939",
            "taskResponser": None,
            "beginTime": start_date_str,
            "endTime": end_date_str,
            "createTime": start_date_str,
            "taskDesp": "检测",
            "taskStatus": "3",
            "updateTime": None,
            "woId": "1",
            "bussIds": assetId,
            "decomposedType": None,
            "decomposedStatus": "",
            "decomposedFlag": "",
            "executeStatus": "",
            "checkTypes": "15",
            "itemRange": None,
            "sysHoleDeviceId": None,
            "sysTempId": None,
            "sysTempName": None,
            "sysTempuuid": None,
            "webHoleDeviceId": None,
            "webTempId": None,
            "webTempName": None,
            "source": None,
            "idLvl": "1",
            "idFlag": id,
            "executeNum": None,
            "scannerDesc": None,
            "taskType": None,
            "scannerParam": None,
            "actualStartTime": None,
            "platForm": '0',
            "cycStr": None,
            "taskClass": "0",
        }
        message1 = render_template("""
           insert into TF_SA_COMPLIANCE_TASK(TASK_ID, TASK_NAME, TASK_SOURCE, TASK_FLAG, MAIN_TASK_ID, TASK_CREATOR,
                                      TASK_RESPONSER, BEGIN_TIME, END_TIME, CREATE_TIME, TASK_DESP,
                                      TASK_STATUS, UPDATE_TIME, WO_ID, TASK_BUSSIDS, DECOMPOSED_TYPE,
                                      DECOMPOSED_STATUS, DECOMPOSED_FLAG, EXECUT_STATUS, ITEM_TYPE, ITEM_RANGE,
                                      SYS_SCANNER_ID, SYS_TEMP, SYS_TEMP_NAME, SYS_UUID, WEB_SCANNER_ID, WEB_TEMP,
                                      WEB_TEMP_NAME, SOURCE, ID_LVL, ID_FLAG, EXECUTE_NUM,
                                      SCANNERDESC, TASK_TYPE, SCANNER_PARAM, ACTUAL_START_TIME, PLAT_FORM, CYC_STR, TASK_CLASS)
    values ($taskId, $taskName, $taskSource, $taskFlag, $mainTaskId, $taskCreator, $taskResponser,
            $beginTime, $endTime, $createTime, $taskDesp,
            $taskStatus, $updateTime, $woId, $bussIds, $decomposedType, $decomposedStatus, $decomposedFlag,
            $executeStatus, $checkTypes, $itemRange,
            $sysHoleDeviceId, $sysTempId, $sysTempName, $sysTempuuid, $webHoleDeviceId, $webTempId,
            $webTempName, $source, $idLvl, $idFlag, $executeNum,
            $scannerDesc, $taskType, $scannerParam, $actualStartTime, $platForm, $cycStr, $taskClass);
        """, seep)

        list.append(message1)

        responseText = json.dumps({
            "urlId": id,
            "checkResult": "1",
            "taskId": id,
            "url": url
        }, ensure_ascii=False)

        report = {
            "id": id,
            "uitraViresTestId": id,
            "menuName": "内容合规检测",
            "menuUrl": url,
            "uitraViresState": "1",
            "responseCode": 200,
            "acount": None,
            "responseText": responseText,
            "executeTime": f"{timeSpan}",
            "createTime": start_date_str,
            "updateTime": end_date_str,
            "payLoad": "",
            "executeState": "",
            "netId": assetId
        }

        message2 = render_template(
            """
                     insert into TF_SA_UITRA_VIRES_MENU (ID, UITRA_VIRES_TEST_ID, MENU_NAME, 
                      MENU_URL, UITRA_VIRES_STATE, RESPONSE_CODE, 
                      ACOUNT, RESPONSE_TEXT, EXECUTE_TIME, 
                      CREATE_TIME, UPDATE_TIME,PAY_LOAD,EXECUTE_STATE,NET_ID)
                    values ($id, $uitraViresTestId, $menuName, 
                      $menuUrl, $uitraViresState, $responseCode, 
                      $acount, $responseText, $executeTime, 
                      $createTime, $updateTime, $payLoad,$executeState,$netId);
                """,
            report)
        list.append(message2)

    write_file('target/SATP_内容合规检测造数.sql', list)
       
